package com.github.tj123.frame.controller;

import com.github.tj123.frame.pojo.po.Subscriber;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
public class FormController {
	

	@RequestMapping(value="/", method=RequestMethod.GET)
	public String loadFormPage(Model m) {
		m.addAttribute("subscriber", new Subscriber());
		return "formPage";
	}
	
	@RequestMapping(value="/haha", method=RequestMethod.GET)
	public Object submitForm(@Valid Subscriber subscriber, @NotBlank String hehe, BindingResult result, Model m) throws Exception{
//		if(result.hasErrors()) {
//			return result.getAllErrors();
//		}
		System.out.println("sss");

		if (result.hasErrors()) {
			throw new BindException(result);
			//return result.getAllErrors();
		}
		
//		m.addAttribute("message", "Successfully saved person: " +);
		return  subscriber;
	}
}
